<script setup>
// 连接服务器
import {onUnmounted} from "vue";

const sseSource = new EventSource("/api/sse/1");

// 连接打开
sseSource.onopen = function () {
  console.log("连接打开");
}

// 连接错误
sseSource.onerror = function (err) {
  console.log("连接错误:", err);
}

// 接收到数据
sseSource.onmessage = function (event) {
  console.log("接收到数据:", event);
  handleReceiveData(event)
}


onUnmounted(()=>{
  sseSource.close()
})



// 处理服务器返回的数据
function handleReceiveData(data) {
  let div = document.createElement('div');
  div.innerHTML = data.data;
  document.getElementById('result').appendChild(div);
}
</script>

<template>
  <div>
    <div id="result"></div>
  </div>
</template>

<style scoped>

</style>
